﻿using UnityEngine;
using System.Collections;

namespace Battle
{
    public delegate void OnComboGuideBegin();
    public delegate void OnComboGuideEnd();
    public class ComboGuidePanel : MonoBehaviour
    {
        public GameObject mask;
        public GameObject tip;
        public UILabel tipLabel;

        public UITexture detailTexture;

        public OnComboGuideBegin onBegin;
        public OnComboGuideEnd onEnd;

        public enum ComboGuideStep
        {
            None,
            First,
            FireBall,
            WaterBall
        }

        public const string guide_fire_ball_path = @"GuidePics/UI_yewai_peitu_01";
        public const string guide_water_ball_path = @"GuidePics/UI_yewai_peitu_02";

        public ComboGuideStep step = ComboGuideStep.None;

        public bool IsShowing = false;

        public void Play()
        {
            step = ComboGuideStep.None;
            StartCoroutine(ResourceLoader.GetInstance().textureLoader.LoadTexture(guide_fire_ball_path, null));
            StartCoroutine(ResourceLoader.GetInstance().textureLoader.LoadTexture(guide_water_ball_path, null));

            OnNextStep();
            if (onBegin != null)
            {
                onBegin();
            }
            IsShowing = true;
        }

        public void OnPanelClick()
        {
            if (step == ComboGuideStep.None)
            {
                return;
            }
            OnNextStep();
        }

        void OnNextStep()
        {
            if (step == ComboGuideStep.None)
            {
                First();
            }
            else if (step == ComboGuideStep.First)
            {
                FireBall();
            }
            else if (step == ComboGuideStep.FireBall)
            {
                WaterBall();
            }
            else
            {
                Over();
            }
        }

        void Update()
        {
            if (step == ComboGuideStep.First)
            {

            }
            else if (step == ComboGuideStep.FireBall)
            {
                if (detailTexture.mainTexture == null)
                {
                    detailTexture.mainTexture = ResourceLoader.GetInstance().textureLoader.GetTexture(guide_fire_ball_path, CommonMethod.GetAssetNameFromAssetBundleName(guide_fire_ball_path)) as Texture2D;
                }
            }
            else if (step == ComboGuideStep.WaterBall)
            {
                if (detailTexture.mainTexture == null)
                {
                    detailTexture.mainTexture = ResourceLoader.GetInstance().textureLoader.GetTexture(guide_water_ball_path, CommonMethod.GetAssetNameFromAssetBundleName(guide_water_ball_path)) as Texture2D;
                }
            }
            else
            {

            }
        }

        void First()
        {
            step = ComboGuideStep.First;
            mask.SetActive(true);
            tip.SetActive(true);
            tipLabel.text = TableTextBlo.GetText("combo_guide_text_1");
            tip.transform.localPosition = new Vector3(420, -180, 0);
            detailTexture.gameObject.SetActive(false);
        }

        void FireBall()
        {
            step = ComboGuideStep.FireBall;
            mask.SetActive(true);
            tip.SetActive(true);
            tipLabel.text = TableTextBlo.GetText("combo_guide_text_2");
            tip.transform.localPosition = new Vector3(420, -180, 0);
            detailTexture.gameObject.SetActive(true);
            detailTexture.mainTexture = null;
        }

        void WaterBall()
        {
            step = ComboGuideStep.WaterBall;
            mask.SetActive(true);
            tip.SetActive(true);
            tipLabel.text = TableTextBlo.GetText("combo_guide_text_3");
            tip.transform.localPosition = new Vector3(420, -180, 0);
            detailTexture.gameObject.SetActive(true);
            detailTexture.mainTexture = null;
        }


        void Over()
        {
            detailTexture.mainTexture = null;
            ResourceLoader.GetInstance().textureLoader.UnloadTexture(@"GuidePics/UI_yewai_peitu_01");
            ResourceLoader.GetInstance().textureLoader.UnloadTexture(@"GuidePics/UI_yewai_peitu_02");
            step = ComboGuideStep.None;

            detailTexture.gameObject.SetActive(false);
            mask.SetActive(false);
            tip.SetActive(false);

            if (onEnd != null)
            {
                onEnd();
            }

            IsShowing = false;
        }


    }
}